package com.google.code.coma.gui.dialog;

import com.google.code.coma.Coma;
import com.google.code.coma.domain.Computer;
import com.google.code.coma.domain.LifeCycleStep;
import com.google.code.coma.domain.Order;
import com.google.code.coma.gui.ComputerCellRenderer;
import com.google.code.coma.gui.util.ImageUtil;
import com.google.code.coma.gui.worker.WorkerManager;
import com.google.code.coma.rmi.ComputerSelector;
import com.google.code.coma.rmi.RMIManager;
import com.google.code.coma.space.ComputerSelectorCallback;
import com.google.code.coma.space.OrderSelectorCallback;
import com.google.code.coma.space.SpaceSelector;
import com.google.code.coma.space.util.SpaceManager;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.mozartspaces.core.*;

import java.net.URI;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Dominik Strasser, dominikstr@gmail.com
 */
public class OrderDialog extends Stage {

	public OrderDialog(Order order, WorkerManager.Mode operationMode, String factoryId, String spaceURI) {
		super();
		if (order == null) return;
		initModality(Modality.APPLICATION_MODAL);
		initStyle(StageStyle.UTILITY);
		setTitle(order.getId());
		setWidth(600);
		setHeight(300);
		BorderPane border = new BorderPane();
		Scene scene = new Scene(border);

		ListView<Computer> computers = new ListView<Computer>();
		computers.setCellFactory(new ComputerCellRenderer());
		computers.setItems(FXCollections.<Computer>observableArrayList(getComputers(order, operationMode, factoryId, spaceURI)));

		VBox info = new VBox();
		info.setSpacing(10);
		info.getChildren().add(new ImageView(ImageUtil.getOrderImage()));
		info.getChildren().addAll(new Label("CPU: " + order.getCpuType()), new Label("RAM: " + order.getNumberOfRAMModules()), new Label("Graphics: " + order.isGraphicsCard()));
		info.getChildren().add(new Label("Finsihed: " + order.isFinished()));

		border.setCenter(computers);
		border.setLeft(info);

		setScene(scene);
		show();
	}

	private List<Computer> getComputers(Order order, WorkerManager.Mode operationMode, String factoryId, String spaceURI) {
		if (operationMode == WorkerManager.Mode.SPACE) {
			MzsCore core = DefaultMzsCore.newInstance(0);
			Capi capi = new Capi(core);
			try {
				ContainerReference containerReference = SpaceManager.getOrCreateNamedContainer(URI.create(spaceURI), factoryId + "_" + Coma.COMPUTER_CONTAINER, capi);
				return capi.read(containerReference, new SpaceSelector(new ComputerSelectorCallback(order, LifeCycleStep.values())), MzsConstants.RequestTimeout.DEFAULT, null);
			} catch (MzsCoreException e) {
				throw new RuntimeException(e);
			}
		} else if (operationMode == WorkerManager.Mode.RMI) {
			try {
				return RMIManager.getStorageService(factoryId + "_" + Coma.COMPUTER_CONTAINER).getSnapshot(new ComputerSelector(order, LifeCycleStep.values()));
			} catch (RemoteException e) {
				throw new RuntimeException(e);
			}
		}
		return new ArrayList<Computer>();
	}
}
